// import { from } from 'core-js/core/array'
import Vue from 'vue'
import VueRouter from 'vue-router'
// import login from "../views/lohin"

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: ()=> import("../views/login.vue")
  },
  {
    path: '/home',
    name: 'home',
    component: ()=> import("../views/home.vue"),
    redirect:'/welcome',
    children:[
      {
        path:"/welcome",
        name:"Welcome",
        component: ()=> import("../components/Welcome.vue"),
      },
      {
        path:"/users",
        name:"Users",
        component: ()=> import("../components/users/Users.vue"),
      },
      {
        path:"/rights",
        name:"Rights",
        component: ()=> import("../components/power/Rights.vue"),
      },
      {
        path:"/roles",
        name:"Roles",
        component: ()=> import("../components/power/Roles.vue"),
      },
      {
        path:"/goods",
        name:"Goods",
        component: ()=> import("../components/power/Goods.vue"),
      },
	  {
	    path:"/goods/add",
	    name:"AddGoods",
	    component: ()=> import("../components/power/Addgoods.vue"),
	  },
      {
        path:"/params",
        name:"Params",
        component: ()=> import("../components/power/Params.vue"),
      },
      {
        path:"/categories",
        name:"Categories",
        component: ()=> import("../components/power/Categories.vue"),
      },
      {
        path:"/orders",
        name:"Orders",
        component: ()=> import("../components/power/Orders.vue"),
      },
      {
        path:"/reports",
        name:"Reports",
        component: ()=> import("../components/power/Reports.vue"),
      },
    ]
  },
  {
    path: '/404',
    name: 'page404',
    component: ()=> import("../views/page404.vue")
  },
  {
    path: '/500',
    name: 'page500',
    component: ()=> import("../views/page500.vue")
  },
  {
    path:"*",
    redirect:"/login"
  },


  
]

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

// 路由守卫
// to: 表示将要访问的路径(route)
// from: 表示从哪里来（route）
// next: 下一个要做的操作
router.beforeEach((to,from,next)=>{
  // 如果用户访问的是login路径，则直接放行
  if(to.path==="/login") return next();
  // 如果是其他路径则需要登录鉴权
  //获取token（判断用户是否登录的重要依据就是是否在本地存储中存在token值
  let token =window.sessionStorage.getItem("token");
  // 如果不存在token值那么直接重定向到登录页面
  if(!token) return next ("/login")
  // token值存在的情况下可以访问任何路径（直接放行）
  next()
})

export default router
